package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcCsunSleepData;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.device.DevicePageQuery;
import com.csun.cmny.provider.model.dto.device.DeviceQueryDto;
import com.csun.cmny.provider.model.dto.device.DeviceReqDto;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.statistics.DmcDeviceStatistics;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.life.DmcDeviceByRoom;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunSleepDataService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.validator.DmcDeviceAddValidator;
import com.csun.cmny.provider.validator.DmcDeviceEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2017-03-01 19:53
 * @Description: the class dmc device main controller.
 */
@RestController
@RequestMapping(value = "/device", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcDeviceMainController extends BaseController {

    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcCsunSleepDataService dmcCsunSleepDataService;

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "添加设备")
    @RequestMapping(value = "/addGroupDevice", method = RequestMethod.POST)
    public Wrapper addDevice(@Validated(DmcDeviceAddValidator.class)
                                 @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);

        dmcDeviceService.addDevice(dmcDevice, getLoginAuthDto());

       // return WrapMapper.ok();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null));
    }

    @RequestMapping(value = "/queryDeviceListWithPage", method = RequestMethod.POST)
    public Wrapper queryDeviceListWithPage(@RequestBody DevicePageQuery devicePageQuery) {

        devicePageQuery.setGroupId(getLoginAuthDto().getGroupId());

        PageInfo pageInfo = dmcDeviceService.queryDeviceListWithPage(devicePageQuery);

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 查询设备列表
     *
     * @return
     */
    @RequestMapping(value = "/queryDeviceList", method = RequestMethod.POST)
    public Wrapper queryDeviceList(@RequestBody DeviceQueryDto deviceQueryDto) {

        List<DmcDevice> dmcDeviceList = dmcDeviceService.queryDeviceList(deviceQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null), dmcDeviceList);

    }

    @ValidateAnnotation
    @RequestMapping(value = "/getDeviceById", method = RequestMethod.POST)
    public Wrapper getDeviceById(Long deviceId) {

        DeviceReqDto deviceReqDto = dmcDeviceService.getDeviceById(deviceId);

       // return WrapMapper.ok(deviceReqDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),deviceReqDto);
    }

//    @LogAnnotation
    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "编辑设备")
    @RequestMapping(value ="/editGroupDevice", method = RequestMethod.POST)
    public Wrapper editDevice(@Validated(value={DmcDeviceEditValidator.class})
                                  @RequestBody DmcEditDeviceDto dmcEditDeviceDto, BindingResult br) {

        DmcDevice dmcDevice = new DmcDevice();

        BeanUtils.copyProperties(dmcEditDeviceDto, dmcDevice);

        Integer result = dmcDeviceService.editDevice(dmcDevice, getLoginAuthDto());

      //  return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }

    /**
     * @description: TODO 查询所有设备的数量
     * @param:
     * @return: 设备总数
     * @throws
     * @author huangxm
     * @date 2019/3/8 14:32
     */
    @RequestMapping(value = "getDeviceSumList",method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "统计设备数量")
    public Wrapper getDeviceSumList() {
        List<DmcDeviceTypeEnum> devices = DmcDeviceTypeEnum.getList();
        Collections.sort(devices,Comparator.comparingInt(DmcDeviceTypeEnum::getIndex));
        List<DmcDeviceStatistics> deviceSumList = new ArrayList<>();
        Long groupId = getLoginAuthDto().getGroupId();
        devices.stream().forEach((o)->{
            Integer deviceSum = dmcDeviceService.getDeviceSum(groupId, o.getIndex());
            DmcDeviceStatistics dmcDeviceStatistics = new DmcDeviceStatistics();
            dmcDeviceStatistics.setDeviceName(o.getValue());
            dmcDeviceStatistics.setDeviceType(o.getIndex());
            dmcDeviceStatistics.setDeviceSum(deviceSum==null?0:deviceSum);
            deviceSumList.add(dmcDeviceStatistics);
        });
      //  return WrapMapper.ok(deviceSumList);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),deviceSumList);
    }

    @ValidateAnnotation
    @RequestMapping(value = "/getDeviceByDeviceNo", method = RequestMethod.POST)
    public Wrapper addDevice(@RequestBody DevicePageQuery devicePageQuery) {
        DmcDevice dmcDevice = new DmcDevice();
        dmcDevice.setDeviceNo(devicePageQuery.getDeviceNo());
        dmcDevice = dmcDeviceService.selectOne(dmcDevice);
       // return WrapMapper.ok(dmcDevice);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcDevice);
    }

    @ValidateAnnotation
    @RequestMapping(value = "/selectDeviceByRoomId/{roomId}", method = RequestMethod.POST)
    public Wrapper selectDeviceByRoomId(@PathVariable Long roomId) {
        List<DmcDeviceByRoom> dmcDevices=dmcDeviceService.selectDeviceByRoomId(roomId,getLoginAuthDto().getGroupId());
        for (DmcDeviceByRoom dmcDevice : dmcDevices) {
            if(dmcDevice.getDeviceType()==DmcDeviceTypeEnum.CSUN_SLEEP.getIndex().longValue()){
                DmcCsunSleepData dmcCsunSleepData = dmcCsunSleepDataService.selectByKey(dmcDevice.getDeviceId());
                dmcDevice.setStatus(dmcCsunSleepData.getStatus());
            }

        }
        //return WrapMapper.ok(dmcDevices);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcDevices);
    }
}
